﻿//
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Linq;

namespace S2_Xxxx_XxxNetApi
{
    public static class CookieHelper
    {
        /// <summary>
        /// 设置cookie，默认是1分钟
        /// </summary>
        /// <param name="controllerBase"></param>
        /// <param name="cookieName"></param>
        /// <param name="cookieValue"></param>
        public static void SetCookie(ControllerBase controllerBase, string cookieName, string cookieValue, int expiresMinutes = 1)
        {
            CookieOptions options = new CookieOptions();
            // 设置过期时间
            options.Expires = DateTime.Now.AddDays(expiresMinutes);
            controllerBase.HttpContext.Response.Cookies.Append(cookieName, cookieValue, options);

            string v = controllerBase.HttpContext.Request.Cookies[cookieName];
        }


        /// <summary>
        /// 获取cookie的值
        /// </summary>
        /// <param name="controllerBase"></param>
        /// <param name="cookieName"></param>
        public static string GetCookie(ControllerBase controllerBase, string cookieName)
        {
            return controllerBase.HttpContext.Request.Cookies[cookieName];
        }

        /// <summary>
        /// 判断cookie是否存在
        /// </summary>
        /// <param name="controllerBase"></param>
        /// <param name="cookieName"></param>
        public static bool IsExistCookie(ControllerBase controllerBase, string cookieName)
        {
            if (string.IsNullOrEmpty(cookieName) || controllerBase == null || controllerBase.HttpContext == null)
            {
                return false;
            }
            else
            {
                string v = controllerBase.HttpContext.Request.Cookies[cookieName];
                if (v.IsNullOrEmpty())
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }

        /// <summary>
        /// 判断cookie是否存在,如果存在，默认增加一分钟
        /// </summary>
        /// <param name="controllerBase"></param>
        /// <param name="cookieName"></param>
        public static bool IsExistCookie(ControllerBase controllerBase, string cookieName, int expiresMinutes = 1)
        {
            if (string.IsNullOrEmpty(cookieName) || controllerBase == null || controllerBase.HttpContext == null)
            {
                return false;
            }
            else
            {
                string v = controllerBase.HttpContext.Request.Cookies[cookieName];
                if (v.IsNullOrEmpty())
                {
                    return false;
                }
                else
                {
                    CookieOptions options = new CookieOptions();
                    options.Expires = DateTime.Now.AddDays(expiresMinutes);
                    controllerBase.HttpContext.Response.Cookies.Append(cookieName, v, options);

                    return true;
                }
            }
        }

        /// <summary>
        /// 删除cookie
        /// </summary>
        /// <param name="controllerBase"></param>
        /// <param name="cookieName"></param>
        public static void DelCookie(ControllerBase controllerBase, string cookieName)
        {
            controllerBase.HttpContext.Response.Cookies.Delete(cookieName);
        }


        /// <summary>
        /// 获取客户Ip
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static string GetClientUserIp(this HttpContext context)
        {
            var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            if (string.IsNullOrEmpty(ip))
            {
                ip = context.Connection.RemoteIpAddress.ToString();
            }
            return ip;
        }

        public static string getClientIP(this HttpContext context)
        {
            string fromSource = string.Empty;
            //test-start
            string s1 = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
            Console.WriteLine("X-Forwarded-For：" + s1);
            string s2 = context.Request.Headers["X-Real-IP"].FirstOrDefault();
            Console.WriteLine("X-Real-IP：" + s2);
            string s3 = context.Request.Headers["Proxy-Client-IP"].FirstOrDefault();
            Console.WriteLine("Proxy-Client-IP：" + s3);
            string s4 = context.Request.Headers["WL-Proxy-Client-IP"].FirstOrDefault();
            Console.WriteLine("WL-Proxy-Client-IP：" + s4);
            //test-end

            var ip = context.Request.Headers["X-Real-IP"].FirstOrDefault();
            if (ip == null || ip.Length == 0 || "unknown".Equals(ip.ToString().ToUpper()))
            {
                ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
                fromSource = "X-Forwarded-For";
              
            }
            if (ip == null || ip.Length == 0 || "unknown".Equals(ip.ToString().ToUpper()))
            {
                ip = context.Request.Headers["Proxy-Client-IP"].FirstOrDefault();
                fromSource = "Proxy-Client-IP";
            }
            if (ip == null || ip.Length == 0 || "unknown".Equals(ip.ToString().ToUpper()))
            {
                ip = context.Request.Headers["WL-Proxy-Client-IP"].FirstOrDefault();
                fromSource = "WL-Proxy-Client-IP";
            }
            //if (ip == null || ip.Length == 0 || "unknown".Equals(ip.ToString().ToUpper()))
            //{
            //   // ip = context.Request.getRemoteAddr();

            //    ip = context.Request.getRemoteAddr();
               
            //    fromSource = "request.getRemoteAddr";
            //}
            return ip;
        }
    }
}
